const app = getApp();
const util = require('../../../utils/util.js')
Page({
  data: {
    typePicker: ['个人', '企业'],
    type:1,
    mobile:'',
    code: '',
    real_name:'',
    contact:'',
    invite_code:'',
    password:'',
    confirm_password:'',
    license_image:'',
    full_license_image:'',
    time:60,
    can_send_code:true,
  },
  BackPage:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  TypeChange(e) {
    this.setData({
      type: e.detail.value
    })
  },

  mobileInput:function(e){
    this.setData({
      mobile:e.detail.value
    })
  },
  codeInput:function(e){
    this.setData({
      code:e.detail.value
    })
  },
  realNameInput:function(e){
    this.setData({
      real_name:e.detail.value
    })
  },
  idCardInput:function(e){
    this.setData({
      id_Card:e.detail.value
    })
  },
  contactInput:function(e){
    this.setData({
      contact:e.detail.value
    })
  },
  passwordInput:function(e){
    this.setData({
      password:e.detail.value
    })
  },
  confirmPasswordInput:function(e){
    this.setData({
      confirm_password:e.detail.value
    })
  },

  formSubmit: function (e) {
    var that = this
    if (that.data.real_name == '') {
      var msg = that.data.type==1?"请输入企业名称":"请输入真实姓名";
      util.fail(msg, that)
      return
    }
    if (that.data.mobile == ''){
      util.fail("请输入手机号码", that)
      return
    }
    if (that.data.code == '') {
      util.fail("请输入验证码", that)
      return
    }
    if (that.data.password == '') {
      util.fail("请输入密码", that)
      return
    }
    if (that.data.confirm_password == '') {
      util.fail("请输入确认密码", that)
      return
    }
    if (that.data.confirm_password != that.data.password) {
      util.fail("确认密码与新密码不一致", that)
      return
    }
    if(that.data.type == 1){
      if (that.data.contact == '') {
        util.fail("请输入业务联系人", that)
        return
      }
      if (that.data.license_image == '') {
        util.fail("请上传营业执照", that)
        return
      }
    }

    util.request('miniprograms/register',
        {
          type:that.data.type,
          mobile:that.data.mobile,
          code:that.data.code,
          real_name:that.data.real_name,
          password:that.data.password,
          confirm_password:that.data.confirm_password,
          contact:that.data.type==1?that.data.contact:'',
          license_image:that.data.type==1?that.data.license_image:'',
          scene:app.globalData.scene,
        },
        function (data) {
          if(data.code == 1){
            util.success();
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/site/login/login',
              })
            },1500)

          }else{
            util.fail(data.msg,that);
          }
        });
  },




  //发送验证码
  code() {
    var that = this;
    if(that.data.mobile == ''){
      util.fail("请输入手机号码", that)
      return
    }
    if (!(/^1[34578]\d{9}$/.test(that.data.mobile))) {
      util.fail("手机号码格式不正确", that)
      return
    }
    util.request('miniprograms/sms',
      { phone: that.data.mobile },
      function (data) {
        if (data.code == 1) {
          that.setData({
            can_send_code:false
          });
          that.time();
          util.success(data.msg);
        } else {
          util.fail(data.msg, that);
        }
      });
  },


  time(){
    var _this = this;
    if(!_this.data.can_send_code){
      var timeout = setInterval(function () {
        if(_this.data.time > 1){
          _this.setData({
            time:_this.data.time-1
          })
        }else{
          _this.setData({
            can_send_code:true,
            time:60,
          })
          clearInterval(timeout);
        }

      },1000)
    }
  },


  /**
   * 上传图片
   * @param callback
   */
  ChooseImage: function () {
    var _this = this;
    util.uploadImage(function (res) {
      if(res.code == 1){
        _this.setData({
          license_image: res.data['file'],
          full_license_image: app.globalData.host + '/uploads/' + res.data['file'],
        });
      }else{
        util.fail(res.msg,_this)
      }
    })
  },

  //浏览图片
  ViewImage(e) {
    wx.previewImage({
      urls:[this.data.full_license_image],
    });
  },

  //删除图片
  DelImg(e) {
    wx.showModal({
      title: '确认',
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '确认',
      success: res => {
        if (res.confirm) {
          this.setData({
            license_image: ''
          })
        }
      }
    })
  },
})
